
//
// 101224 - AH - Created.
// 101225c- AH - Now constructors accepts SoekratEngine and HistoricalEvent
//               objects are generated.
//

package org.soekrat.gui.swing.forest;

import static org.soekrat.gui.swing.SoekratSwingUtil.showUserInputErrDlg;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;

import org.soekrat.engine.Soekrat2Engine;
import org.soekrat.engine.forest.Tree;

/**
 * <p>
 *   Dialog for editing a
 *   {@link Tree}.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC) 
 */
@SuppressWarnings("serial")
public final class EditTreeDialog extends JDialog 
{
  private final CreateEditTreePanel m_cetp=new CreateEditTreePanel();
  
  private boolean m_edited=false;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param engine {@link Soekrat2Engine} interfaced.
   */
  public EditTreeDialog(Soekrat2Engine engine){ buildGui(engine); }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param p      Dialog's parent.
   * @param engine {@link Soekrat2Engine} interfaced. 
   */
  public EditTreeDialog(JDialog p, Soekrat2Engine engine)
  { 
    super(p); 
    buildGui(engine); 
  }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param p      Dialog's parent.
   * @param engine {@link Soekrat2Engine} interfaced. 
   */
  public EditTreeDialog(JFrame p, Soekrat2Engine engine)
  { 
    super(p); 
    buildGui(engine); 
  }
  
  // Invoked from constructors to build gui.
  private void buildGui(final Soekrat2Engine engine)
  {
    if (engine==null)
    {
      throw new NullPointerException("engine");
    }
    
    setModal(true);
    setTitle("Edit Tree");
    
    Container c=getContentPane();
    c.setLayout(new BorderLayout());
    
    c.add(m_cetp, BorderLayout.CENTER);
    
    JPanel buttons=new JPanel(new FlowLayout(FlowLayout.CENTER));
    buttons.add
    (
      new JButton
      (
        new AbstractAction("OK") 
        {
          public void actionPerformed(ActionEvent e) 
          {
            String rv=m_cetp.edit();
            if (rv==null)
            {
              m_edited=true;
              dispose();
            }
            else if (rv.length()==0)
            {
              dispose();
            }
            else
            {
              showUserInputErrDlg(EditTreeDialog.this, rv);
            }
          }
        }
      )
    );
    buttons.add
    (
      new JButton
      (
        new AbstractAction("Cancel") 
        {
          public void actionPerformed(ActionEvent e){ dispose(); }
        }
      )
    );
    c.add(buttons, BorderLayout.SOUTH);
    
    pack();
  }
  
  /**
   * <p>
   *   Shows dialog.
   * </p>
   * @param tree {@link Tree} to edit.
   * @return {@code true} if change done, {@code false} if none was.
   */
  public boolean showDialog(Tree tree)
  {
    if (tree==null)
    {
      throw new NullPointerException("tree");
    }
    
    m_edited=false;
    m_cetp.setTreeToEdit(tree);
    setVisible(true);
    return m_edited;
  }
  
}
